package com.zsn.servlet;

import com.zsn.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zsn.util.UserUtils;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet  {



    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //输入的数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //判断表中是否有的标识
        boolean flag = false;
        try {
            flag = UserUtils.updateUser(new User(username,password));
        }  catch (Exception e) {
            e.printStackTrace();
        }
        //为true登录成功，false继续登录
        if(flag){
            resp.sendRedirect("main.html");
        }else{
            resp.sendRedirect("login.html");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if("admine".equals(username)&&"123456".equals(password)){
            resp.sendRedirect("main.html");
        }else{
            resp.sendRedirect("login.html");
        }
    }
}

